package com.ymatou.iapi.platformdiscounts.testcase;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;

import org.joda.time.DateTime;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.ymatou.iapi.platformdiscounts.parameter.AddBean;
import com.ymatou.iapi.platformdiscounts.parameter.CalculateBean;
import com.ymatou.iapi.platformdiscounts.parameter.DiscountInfo;
import com.ymatou.iapi.platformdiscounts.parameter.DiscountInfo.ProductDiscountsInfo;
import com.ymatou.iapi.platformdiscounts.parameter.ProductDto;
import com.ymatou.iapi.platformdiscounts.service.AddCall;
import com.ymatou.iapi.platformdiscounts.service.CalculateCall;
import com.ymatou.iapi.proditem.service.GetProductInfoByProductIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DiscountsCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.discount_info;
import com.ymttest.database.model.discount_qualification;
import com.ymttest.database.redis.discountsWapper;
import com.ymttest.database.sqlwapper.discount_infoWapper;
import com.ymttest.database.sqlwapper.discount_qualificationWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*计算商品优惠金额
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_calculate{
	private static CalculateBean bean;
	private static CalculateCall call;
	private static DiscountsCallService callService=new  DiscountsCallService();
	private discount_qualificationWapper dqWapper=new discount_qualificationWapper();
	private SellerProductCallService sellerProuctService=new SellerProductCallService();
	private discount_infoWapper infoWapper=new discount_infoWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("计算商品优惠金额");
	}

	@Before
	public void caseUp() {
		bean=new CalculateBean();
		call=new CalculateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_calculate_001() {
		Logger.start(true,"正例-计算商品优惠金额-验证不传SellerId");
		try {
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			bean.setUserId(addBean.getUserId());
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			dto.setPrice(new BigDecimal(price));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			//dto.setSellerId(obj.getInt("SellerId"));
			
			products.add(dto);
			bean.setProducts(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();	
			Thread.sleep(5000);
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_calculate_001_1() {
		Logger.start(true,"正例-计算商品优惠金额-验证传SellerId");
		try {
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			bean.setUserId(addBean.getUserId());
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			dto.setPrice(new BigDecimal(price));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			dto.setSellerId(obj.getInt("SellerId"));
			
			products.add(dto);
			bean.setProducts(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();	
			Thread.sleep(5000);
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_calculate_001_2() {
		Logger.start(true,"正例-计算商品优惠金额-验证传的SellerId与product不一致");
		try {
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			bean.setUserId(addBean.getUserId());
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			dto.setPrice(new BigDecimal(price));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			dto.setSellerId(3383);
			
			products.add(dto);
			bean.setProducts(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();	
			Thread.sleep(5000);
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_calculate_001_3() {
		Logger.start(true,"正例-计算商品优惠金额-验证传的SellerId不存在");
		try {
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			bean.setUserId(addBean.getUserId());
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			dto.setPrice(new BigDecimal(price));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			dto.setSellerId(11);
			
			products.add(dto);
			bean.setProducts(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();	

			Logger.verifyEquals("0", call.getErrorCode(), "验证返回ErrorCode");
			Logger.verifyEquals("null", call.getErrorMsg(), "验证返回ErrorMsg");
			//验证数据库
			ProductDiscountsInfo respInfos=call.getCallData();
			Logger.verifyEquals(0, respInfos.getProductDiscounts().size(), "验证返回长度与请求商品长度一致");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_calculate_002() {
		Logger.start(true,"正例-计算商品优惠金额-同一规格，不同商品 ");
		try {
			//新增第一个优惠资格
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			//新增第二个优惠资格
			AddBean addBean2=callService.setAdd();
			addBean2.setSourceId(TestDataManager.getRandomIntNum(5));
			addBean2.setUserId(addBean.getUserId());
			
			AddCall addCall2=callService.callAdd(addBean2);
			
			bean.setUserId(addBean.getUserId());
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			ProductDto dto2=new ProductDto();
			//计算第一个优惠资格
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			
			dto.setPrice(new BigDecimal(price));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			products.add(dto);
			//计算第二个优惠资格
			JSONObject obj2 = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean2.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price2= new JSONObject(JSONArray.fromObject(obj2.getString("CatalogList")).get(0).toString()).getString("Price");
			dto2.setProductId(addBean2.getProductId());
			
			dto2.setPrice(new BigDecimal(price2));	
			dto2.setsecondCategoryId(obj2.getInt("CategoryId"));
			products.add(dto2);
			bean.setProducts(products);

			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();		
			Thread.sleep(5000);
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_calculate_002_2() {
		Logger.start(true,"正例-计算商品优惠金额-同一规格，同一商品 (计算两遍)");
		try {
			//新增第一个优惠资格
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			
			bean.setUserId(addBean.getUserId());
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			ProductDto dto2=new ProductDto();
			//计算第一个优惠资格
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			
			dto.setPrice(new BigDecimal(price));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			products.add(dto);
			//计算第二个优惠资格
			JSONObject obj2 = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price2= new JSONObject(JSONArray.fromObject(obj2.getString("CatalogList")).get(0).toString()).getString("Price");
			dto2.setProductId(addBean.getProductId());
			
			dto2.setPrice(new BigDecimal(price2));	
			dto2.setsecondCategoryId(obj2.getInt("CategoryId"));
			products.add(dto2);
			bean.setProducts(products);

			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();	
			Thread.sleep(5000);
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_calculate_002_1() {
		Logger.start(true,"正例-计算商品优惠金额-同商品，2个不同规格");
		try {
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			bean.setUserId(addBean.getUserId());
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			dto.setPrice(new BigDecimal(price));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			products.add(dto);
			bean.setProducts(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();			
			Thread.sleep(5000);
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_calculate_003() {
		Logger.start(false,"反例-计算商品优惠金额-验证商品优惠已使用，再次计算，discount_info表只插入一次");
		try {
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			bean.setUserId(addBean.getUserId());
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			dto.setPrice(new BigDecimal(price));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			products.add(dto);
			bean.setProducts(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();	
			call.callService();	
			Thread.sleep(5000);
			verifyReturn();
			//验证只有一条数据
			discount_info record=new discount_info();
			record.setUserId(addBean.getUserId());
			record.setProductId(bean.getProducts().get(0).getProductId());
			int count=infoWapper.selectCount(record);
			Logger.verifyEquals(1, count, "验证discount_info表只插入一次");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_calculate_004() {
		Logger.start(true,"正例-计算商品优惠金额-未使用已过期");
		try {
			Date dNow = new Date(); // 当前时间
	        Date dAfter = new Date();

	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.HOUR_OF_DAY, -1); // 设置为前一小时
	        dAfter = calendar1.getTime(); 
	        
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			
			//修改过去时间
			discount_qualification dqinfo=dqWapper.selectTop1(Long.toString(addBean.getSourceId()),Integer.toString(addBean.getUserId()), addBean.getProductId());
			dqinfo.setExpireTime(dAfter);
			dqWapper.updateByPrimaryKey(dqinfo);
			
			bean.setUserId(addBean.getUserId());
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			dto.setPrice(new BigDecimal(price));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			products.add(dto);
			bean.setProducts(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();			
			Thread.sleep(5000);
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_calculate_005() {
		Logger.start(false,"反例-计算商品优惠金额-砍价团不可享受该优惠");
		try {
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			bean.setUserId(addBean.getUserId());
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			dto.setPrice(new BigDecimal(price));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			products.add(dto);
			bean.setProducts(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();		
			Thread.sleep(5000);
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_calculate_006() {
		Logger.start(false,"反例-计算商品优惠金额-商品没有优惠资格");
		try {

			int userId=TestDataManager.getRandomIntNum(5);
			String productId=new DiscountsCallService().createProduct().getString("ProductId");
			bean.setUserId(userId);
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(productId).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(productId);
			dto.setPrice(new BigDecimal(price));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			products.add(dto);
			bean.setProducts(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();			

			discount_info record=new discount_info();
			record.setUserId(bean.getUserId());
			record.setProductId(bean.getProducts().get(0).getProductId());
			discount_info DBinfo=infoWapper.selectTop1(record);
			
			Logger.verifyEquals(null, DBinfo.getDiscountId(), "验证discount_info表没有插入成功");
			Logger.verifyEquals("0", call.getErrorCode(), "验证返回ErrorCode");
			Logger.verifyEquals("null", call.getErrorMsg(), "验证返回ErrorMsg");
			
			ProductDiscountsInfo respInfos=call.getCallData();
			Logger.verifyEquals(0, respInfos.getProductDiscounts().size(), "验证ProductDiscounts 返回为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_calculate_007() {
		Logger.start(true,"正例-计算商品优惠金额-验证未使用已过期，未到下次优惠时间");
		try {
			discountsWapper disWapper=new discountsWapper();
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			bean.setUserId(addBean.getUserId());
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			dto.setPrice(new BigDecimal(600));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			products.add(dto);
			bean.setProducts(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();	
			
			Thread.sleep(10000);
			
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date dNow = new Date(); // 当前时间
	        Date dAfter = new Date();
	        Date dAfter2 = new Date();
	        Date dAfter3 = new Date();

	        Calendar calendar1 = Calendar.getInstance(); // 得到日历
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.HOUR_OF_DAY, -10); // 设置为前一小时
	        dAfter = calendar1.getTime(); 
	        
	        Calendar calendar2 = Calendar.getInstance(); // 得到日历
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.HOUR_OF_DAY, -1); // 设置为前5小时
	        dAfter2 = calendar2.getTime();
	        
	        
	        Calendar calendar3 = Calendar.getInstance(); // 得到日历
	        calendar3.setTime(dNow);// 把当前时间赋给日历
	        calendar3.add(Calendar.HOUR_OF_DAY, 5); // 设置为前4小时
	        dAfter3 = calendar3.getTime();
	        
			//修改redis时间
	        String str=disWapper.getDiscountTime(addBean.getUserId());
	        String did=str.substring(str.lastIndexOf(":")+1, str.length()-1);
	       
	        String updateStr="{\"ndt\":\""+formatter.format(dAfter3).replace(" ", "T")+"\",\"et\":\""+formatter.format(dAfter2).replace(" ", "T")+"\",\"did\":"+did+"}";
	        //String updateStr="{\"ndt\":\""+ DateTime.now().plusHours(1).toDate()+"\",\"et\":\""+ DateTime.now().minusHours(1).toDate()+"\",\"did\":"+did+"}";
	      //  disWapper.setDiscountTime(addBean.getUserId(),updateStr);
	        
	        discount_info info=new discount_info();
	        info.setUserId(addBean.getUserId());
	        info.setProductId(addBean.getProductId());
	        discount_info dqinfo=infoWapper.selectTop1(info );
	        
			dqinfo.setExpireTime(dAfter);
			dqinfo.setExpiredNextDiscountTime(dAfter2);
			dqinfo.setUsedNextDiscountTime(dAfter3);
			infoWapper.updateByPrimaryKey(dqinfo);
			Thread.sleep(5000);
			//call.setData(bean);
			
		//	bean.getProducts().get(0).setProductId(bean.getProducts().get(0).getProductId()+"1");
			call.callService();		
		//	verifyReturn();
			Logger.verifyEquals("0", call.getErrorCode(), "验证返回ErrorCode");
			Logger.verifyEquals("null", call.getErrorMsg(), "验证返回ErrorMsg");
			
			ProductDiscountsInfo respInfos=call.getCallData();
			Logger.verifyEquals(0, respInfos.getProductDiscounts().size(), "验证ProductDiscounts 返回为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
//	@Test
//	@Category({P1.class})
//	@TestCase
//	//@Ignore
//	public void Tc_calculate_007_1() {
//		Logger.start(true,"正例-计算商品优惠金额-验证已使用，未到下次优惠时间");
//		try {
//			discountsWapper disWapper=new discountsWapper();
//			AddBean addBean=callService.setAdd();
//			AddCall addCall=callService.callAdd(addBean);
//			bean.setUserId(addBean.getUserId());
//			
//			List<ProductDto> products=new ArrayList();
//			ProductDto dto=new ProductDto();
//			JSONObject obj = GetProductInfoByProductIdCall
//					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
//					.getJSONObject("Product");
//			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
//			dto.setProductId(addBean.getProductId());
//			dto.setPrice(new BigDecimal(price));	
//			dto.setsecondCategoryId(obj.getInt("CategoryId"));
//			products.add(dto);
//			bean.setProducts(products);
//			bean.setAppId("11");
//			bean.setRequestId("22");
//			call.setData(bean);
//			call.callService();		
//			
//			
//			verifyReturn();
//			
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_calculate_008() {
		Logger.start(true,"正例-计算商品优惠金额-优惠金额小于最低金额下限");
		try {
			AddBean addBean=callService.setAdd();
			AddCall addCall=callService.callAdd(addBean);
			bean.setUserId(addBean.getUserId());
			
			List<ProductDto> products=new ArrayList();
			ProductDto dto=new ProductDto();
			JSONObject obj = GetProductInfoByProductIdCall
					.GetProductInfoByProductId(addBean.getProductId()).getJSONObject("Data")
					.getJSONObject("Product");
			String  price= new JSONObject(JSONArray.fromObject(obj.getString("CatalogList")).get(0).toString()).getString("Price");
			dto.setProductId(addBean.getProductId());
			dto.setPrice(new BigDecimal(10));	
			dto.setsecondCategoryId(obj.getInt("CategoryId"));
			products.add(dto);
			bean.setProducts(products);
			bean.setAppId("11");
			bean.setRequestId("22");
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("0", call.getErrorCode(), "验证返回ErrorCode");
			Logger.verifyEquals("null", call.getErrorMsg(), "验证返回ErrorMsg");
			
			ProductDiscountsInfo respInfos=call.getCallData();
			Logger.verifyEquals(0, respInfos.getProductDiscounts().size(), "验证ProductDiscounts 返回为空");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyReturn()
	{
	//	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
		Logger.verifyEquals("0", call.getErrorCode(), "验证返回ErrorCode");
		Logger.verifyEquals("null", call.getErrorMsg(), "验证返回ErrorMsg");
		//验证数据库
		ProductDiscountsInfo respInfos=call.getCallData();
		Logger.verifyEquals(bean.getProducts().size(), respInfos.getProductDiscounts().size(), "验证返回长度与请求商品长度一致");
		for(int i=0;i<respInfos.getProductDiscounts().size();i++)
		{
			DiscountInfo respInfo=respInfos.getProductDiscounts().get(i);
			discount_info record=new discount_info();
			record.setUserId(bean.getUserId());
			record.setProductId(bean.getProducts().get(i).getProductId());
			discount_info DBinfo=infoWapper.selectTop1(record);
			
			Logger.verifyEquals(respInfo.getProductId(), DBinfo.getProductId(), "验证第"+(i+1)+"条数据库discount_info 字段ProductId");
			Logger.verifyEquals(respInfo.getDiscountAmount().stripTrailingZeros().toPlainString(), DBinfo.getSingleDiscountAmount().stripTrailingZeros().toPlainString(), "验证第"+(i+1)+"条数据库discount_info 字段DiscountTotalAmount");
			Logger.verifyEquals(true,calLastedTime(DBinfo.getStartTime(),respInfo.getStartTime())<10, "验证第"+(i+1)+"条数据库discount_info 字段StartTime");
			Logger.verifyEquals(true,calLastedTime(DBinfo.getExpireTime(),respInfo.getExpireTime())<10 , "验证第"+(i+1)+"条数据库discount_info 字段ExpireTime");
		}
		
	}
	//计算日期误差，返回的StartTime、ExpireTime是直接从计算服务中获取，数据库的字段后插入，所以存在日期误差
	public  int calLastedTime(Date startDate,Date endDate) {
		  long a = endDate.getTime();
		  long b = startDate.getTime();
		  int c = (int)((a - b) / 1000);
		  return c;
		 }
}